/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package components;

import PCR.MainFrame;
import graphics.GraphicsEngin;
import java.awt.Cursor;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author asmaa
 */
public class BandShape extends JLabel {

    BandTipText bTipText;
    String name;
    BandMenu bMenu = new BandMenu();

    public BandShape() {

        movable = true;
        this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        this.setComponentPopupMenu(bMenu);
        this.setInheritsPopupMenu(true);
        bTipText = new BandTipText();
        MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
        frm.getPCR().getWorkArea().getBandsPanel().add(bTipText);
        bTipText.setVisible(false);

        this.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                bMenu.setCurrentBand(BandShape.this);
                System.out.println("You Clicked on " + name + " Image");
                MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
                frm.setSelectedShape(BandShape.this);

            }

            @Override
            public void mousePressed(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    bMenu.show(e.getComponent(), e.getX(), e.getY());
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                bMenu.setCurrentBand(BandShape.this);
                BandShape.this.bTipText.setVisible(false);

            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });
        this.addMouseMotionListener(new MouseMotionAction());
        this.addKeyListener(new KeyAction());
    }

    class MouseMotionAction implements MouseMotionListener {

        @Override
        public void mouseDragged(MouseEvent e) {

            if (movable) {
                JLabel curBand = (JLabel) e.getSource();
                PointerInfo a = MouseInfo.getPointerInfo();
                Point pt = a.getLocation();
                MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
                SwingUtilities.convertPointFromScreen(pt, frm.getPCR().getWorkArea().getBandsPanel());
                BandShape.this.setLocation(BandShape.this.getLocation().x, pt.y - curBand.getSize().height / 2);
                frm.getCurrentBandPosition("Current Band Position [X: " + Integer.toString(BandShape.this.getLocation().x) + ", Y: " + Integer.toString(pt.y - curBand.getSize().height / 2) + "]");
                BandShape.this.setToolTipText("Current Band Position [X: " + Integer.toString(BandShape.this.getLocation().x) + ", Y: " + Integer.toString(pt.y - curBand.getSize().height / 2) + "]");
                BandShape.this.bTipText.setVisible(true);
                BandShape.this.bTipText.setLocation(curBand.getLocation().x + curBand.getSize().width, pt.y - curBand.getSize().height+20 );
                BandShape.this.bTipText.setTipText("[X: " + Integer.toString(BandShape.this.getLocation().x) + ", Y: " + Integer.toString(pt.y - curBand.getSize().height / 2) + "]");
            }
        }

        @Override
        public void mouseMoved(MouseEvent e) {
        }
    }

    class KeyAction implements KeyListener {

        @Override
        public void keyTyped(KeyEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
        int curY = 0;

        @Override
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();

            switch (keyCode) {
                case KeyEvent.VK_UP:
                    curY -= 10;
                    BandShape.this.setLocation(BandShape.this.getLocation().x, curY);
                    break;
                case KeyEvent.VK_DOWN:
                    curY += 10;
                    BandShape.this.setLocation(BandShape.this.getLocation().x, curY);
                    break;
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    public void setName(String name) {
        this.name = name;
        // ImageIcon image = new ImageIcon(this.name);
        // this.setIcon(image);
        // System.out.println(name);
    }

    public String getName() {
        return name;
    }
    private ImageIcon image;

    public void setImage(ImageIcon img) {
        image = img;
        this.setIcon(img);
    }

    public ImageIcon getImage() {
        return image;
    }
    private boolean movable;

    public void setMovable(boolean mov) {
        movable = mov;
    }

    public boolean isMovable() {
        return movable;
    }
}
